#include <my_header.h>
/*Usage:02_creatfile_w  */
int main(int argc, char *argv[]){
    //设置int类型用来表示要传入pipe中那个参数的长度
    int file_len = 0;
    int context_len = 0;
    int pipe_fd = open("1.pipe",O_RDONLY);
    //先传入文件名字的长度
    ssize_t ret = read(pipe_fd, &file_len, sizeof(int));
    ERROR_CHECK(ret , -1, "read file_name_len error");
    //传入文件的名字
    char filename[60] = { 0 };
    ssize_t sret = read(pipe_fd, filename, file_len);
    ERROR_CHECK(sret, -1, "write file_name error");

    //创建文件
    ssize_t fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
     ERROR_CHECK(fd, -1, "creat file error");
    //先传入文内容的长度
    ssize_t ret1 = read(pipe_fd, &context_len, sizeof(int));
    ERROR_CHECK(ret1, -1, "read context_len error");
    //传入文件的内容
    char str[1024] = { 0 };
    //传入文件内容进入文件
    ssize_t bytes;
    while((bytes = read(pipe_fd, str, sizeof(str))) > 0){
            write(fd, str, bytes); 
    }
    ERROR_CHECK(bytes, -1, "write txt error");
    printf("file_name:%s\n",filename);
    printf(" context: %s\n", str);
    close(fd);
    close(pipe_fd);

    return 0;
}


